#include "NodePriQ.h"

void NodePriQ::Insert(int node)
{
    if(m_nodes.empty())
        m_nodes.push_back(node);
    else
    {
        lInt::iterator it = m_nodes.begin();
        while(it != m_nodes.end())
        {
            if(m_costs[*it] > m_costs[node])
            {
                m_nodes.insert(it, node);
                return;
            }
            ++it;
        }
        //we are here, this node has a large cost and should go to the back
        m_nodes.push_back(node);
    }
}

void NodePriQ::Pop()
{
    m_nodes.pop_front();
}

const int& NodePriQ::Front()const 
{
    return m_nodes.front();
}

void NodePriQ::Resort()
{
    lInt newList;
    
    while(m_nodes.size() > 0)
    {
        lInt::iterator it = m_nodes.begin();
        if(newList.empty())
            newList.push_back((*it));
        else
        {
            lInt::iterator jt = newList.begin();
            bool added = false;
            while(jt != newList.end())
            {
                if(m_costs[*it] < m_costs[(*jt)])
                {
                    added = true;
                    newList.insert(jt, (*it));
                    jt = newList.end();
                }
                if(!added)
                    ++jt;   
            }
            //we are here, this node has a large cost and should go to the back
            if(!added)
                newList.push_back(*it);
        }
        m_nodes.pop_front();
    }

    m_nodes = newList;
}